package dao

import (
	"uwbwebapp/pkg/entities"
)

// 枚举指定游泳者的UWB标签记录
func EnumSwimmerUWBTerminalRecords(swimmerId string) ([]entities.SwimmerUWBTerminalRecord, error) {
	var records []entities.SwimmerUWBTerminalRecord
	err := Database.Table("swimmer_uwb_terminal_records").
		Where("swimmer_id = ?", swimmerId).
		Order("begin_datetime ASC").
		Find(&records).Error
	if err != nil {
		return nil, err
	}
	return records, nil
}

// 枚举指定游泳者指定时间段的UWB标签记录
func EnumSwimmerUWBTerminalRecordsByDateScope(swimmerId string, dateTime entities.BetweenDatetime) ([]entities.SwimmerUWBTerminalRecord, error) {
	var records []entities.SwimmerUWBTerminalRecord
	err := Database.Table("swimmer_uwb_terminal_records").
		Where("swimmer_id = ? AND create_datetime BETWEEN ? AND ?", swimmerId, dateTime.BeginDatetime, dateTime.EndDatetime).
		Order("begin_datetime ASC").
		Find(&records).Error
	if err != nil {
		return nil, err
	}
	return records, nil
}

// 写入记录
func WriteUWBTerminalRecord(record *entities.SwimmerUWBTerminalRecord) error {
	err := Database.Create(record).Error
	return err
}
